Page({
    data: {
      phone: '',
      password: ''
    },
  
    onPhoneInput(e) {
      this.setData({ phone: e.detail.value });
    },
  
    onPasswordInput(e) {
      this.setData({ password: e.detail.value });
    },
  
    onLogin() {
        if (!this.data.phone || !this.data.password) {
          wx.showToast({ title: '请输入手机号和密码', icon: 'none' });
          return;
        }
      
        wx.request({
          url: 'http://localhost:8081/parent/login',
          method: 'POST',
          data: {
            tel: this.data.phone,
            password: this.data.password
          },
          success: (res) => {
            console.log("登录成功，返回数据:", res.data); // 🔥 调试点1：确认后端返回数据
      
            if (res.statusCode === 200) {
              if (!res.data.username) {
                console.error("后端未返回 username");
                wx.showToast({ title: "登录失败，未返回用户名", icon: "none" });
                return;
              }
      
              wx.setStorageSync('token', res.data.token);
              wx.setStorageSync('username', res.data.username);
              wx.setStorageSync('tel', this.data.phone);  // ✅ 存储手机号
              wx.setStorageSync('avatar', res.data.avatar || "/assets/images/avatar.png"); // ✅ 存储头像
              wx.setStorageSync('parentId', res.data.parentId || "");  // ✅ 存储 parentId
              console.log("存储 username:", wx.getStorageSync("username")); // 🔥 调试点2：确认 `username` 是否正确存储
      
              wx.showToast({ title: '登录成功', icon: 'success' });
              wx.switchTab({ url: '/pages/index/index' });
            } else {
              wx.showToast({ title: res.data.message || '登录失败', icon: 'none' });
            }
          },
          fail: (err) => {
            console.error("请求失败:", err);
            wx.showToast({ title: "网络错误，请检查服务器", icon: "none" });
          }
        });
      },
      
      
  
      onForgotPassword() {
        wx.navigateTo({ url: '/pages/login/forgotPassword/forgotPassword' }); // ✅ 修改路径
      },
      
      onRegister() {
        wx.navigateTo({ url: '/pages/login/register/register' }); // ✅ 修改路径
      }
      
  });
  